<template>
    <div class="app-container">
        <el-row style="height: 100%">
            <el-form :model="queryParams" ref="queryForm" size="small" :inline="true">
                <el-form-item label="商品名称">
                    <el-input clearable v-model="queryParams.productName" placeholder="请输入商品名称" clearable
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="快递单号">
                    <el-input clearable v-model="queryParams.trackingNumber" placeholder="请输入快递单号" clearable
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="所在区域" prop="area">
                    <!--省市三级联动-->
                    <el-cascader style="width: 230px;" clearable placeholder="请选择" size="small" v-model="location"
                        :options="areaSelectData" :props="{
                            label: 'Name',
                            value: 'Name',
                            children: 'ChildList',
                            checkStrictly: false   // 可选：允许不选到最后一级
                        }"></el-cascader>
                </el-form-item>
                <el-form-item label="详细地址">
                    <el-input clearable v-model="queryParams.district" placeholder="请输入详细地址" clearable
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="收货人姓名">
                    <el-input clearable v-model="queryParams.receiverName" style="width: 120px;" placeholder="收货人姓名"
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="收货人手机号">
                    <el-input clearable v-model="queryParams.receiverPhone" style="width: 150px;" placeholder="收货人手机号"
                        @keyup.enter.native="handleQuery" />
                </el-form-item>
                <el-form-item label="支付状态">
                    <el-select clearable v-model="queryParams.paymentStatus" placeholder="请选择" style="width: 150px;">
                        <el-option v-for="item in optionsStatus" :key="item.value" :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="物流状态">
                    <el-select clearable v-model="queryParams.logisticsStatus" placeholder="请选择" style="width: 150px;">
                        <el-option v-for="item in logisticsStatusOpt" :key="item.value" :label="item.label"
                            :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="起止时间">
                    <el-date-picker v-model="dateRange" style="width: 350px;" size="small"
                        value-format="yyyy-MM-dd HH:mm:ss" type="datetimerange" range-separator="-"
                        start-placeholder="开始日期" end-placeholder="结束日期" clearable></el-date-picker>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>

            <el-table v-loading="loading" height="83%" :data="userList">
                <el-table-column label="订单编号" align="center" prop="orderNo" />
                <el-table-column label="商品名称" align="center" prop="name" />
                <el-table-column label="商品金额" align="center" prop="totalAmount" />
                <el-table-column label="用户姓名" align="center" prop="receiverName" />
                <el-table-column label="手机号码" align="center" prop="receiverPhone" />
                <el-table-column label="详细地址" align="center" prop="province">
                    <template slot-scope="scope">
                        <span>{{ scope.row.province }}{{ scope.row.city }}{{ scope.row.district
                            }}{{ scope.row.detailAddress }} </span>
                    </template>
                </el-table-column>
                <el-table-column label="商品图片" align="center" prop="province">
                    <template slot-scope="scope">
                        <el-image style="width: 60px; height: 60px" :src="scope.row.imageUrl" fit="cover"></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="订单状态" align="center">
                    <template slot-scope="scope">
                        <span>{{ scope.row.paymentStatus === 0 ? '未支付' : scope.row.paymentStatus === 1 ?
                            '已支付' :
                            scope.row.paymentStatus === 2 ? '已退款' : scope.row.paymentStatus === 3
                                ? '支付失败' : scope.row.paymentStatus === 4 ? '支付超时' : '' }} </span>
                    </template>
                </el-table-column>
                <el-table-column label="快递单号" align="center" prop="trackingNumber" />
                <el-table-column label="支付时间" align="center" width="150" prop="paymentTime" />
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">
                        <el-button v-if="scope.row.paymentStatus === 1" type="primary" size="small"
                            @click="open(scope.row)">发货</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList" />
        </el-row>
    </div>
</template>

<script>
import { getPurchaseListApi, updateTrackingNumberApi } from "@/api/index.js"
import { cityData } from "@/utils/select_area.js"
console.log(cityData, 'areaSelectData');

export default {
    name: "mallOrder",
    components: {},
    data() {
        return {
            location: [],
            areaSelectData: cityData,//省市区三级联动数据
            // 遮罩层
            loading: true,
            // 总条数
            total: 0,
            // 用户表格数据
            userList: null,
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            // 表单参数
            form: {},
            // 查询参数
            queryParams: {
                pageNum: 1,
                pageSize: 10,
                userName: undefined,
                productName: undefined,
                startDate: "",
                endDate: "",
                paymentStatus: undefined,
                logisticsStatus: undefined,
                trackingNumber: undefined,
                province: undefined,
                city: undefined,
                district: undefined,
                detailAddress: undefined,
                receiverPhone: undefined,
                receiverName: undefined,
            },
            dateRange: [],
            optionsStatus: [
                { value: 0, label: "未支付" },
                { value: 1, label: "已支付" },
                { value: 2, label: "已退款" },
                { value: 3, label: "支付失败" },
                { value: 4, label: "支付超时" },
            ],
            logisticsStatusOpt: [
                { value: 0, label: "待付款" },
                { value: 1, label: "待发货" },
                { value: 2, label: "已发货" },
                { value: 3, label: "已签收" },
            ],
        }
    },
    watch: {

    },
    created() {
        this.getList()
    },
    methods: {
        handleChange(value) {
            console.log(value, 'value');

            if (value.length > 0) {
                this.queryParams.province = CodeToText[value[0]];
            } else {
                this.queryParams.province = undefined;
            }
            if (value.length > 1) {
                this.queryParams.city = CodeToText[value[1]];
            } else {
                this.queryParams.city = undefined;
            }
            if (value.length > 2) {
                this.queryParams.district = CodeToText[value[2]];
            } else {
                this.queryParams.district = undefined;
            }
        },
        /** 查询用户列表 */
        getList() {
            this.loading = true
            getPurchaseListApi(this.queryParams).then(response => {
                console.log(response, 'response');
                if (response.data && response.data.list) {
                    this.userList = response.data.list;
                } else {
                    this.userList = []
                }
                this.total = response.data.total
                this.loading = false
            }).catch(() => {
                this.loading = false
            })
        },
        open(item) {
            this.$prompt('请输入快递单号', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                inputPattern: /.+/, // 只要非空即可
                inputErrorMessage: '快递单号不能为空'
            }).then(({ value }) => {
                this.updateTrackingNumber(item.id, value);
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '取消输入'
                });
            });
        },
        // 更新快递单号
        async updateTrackingNumber(id, trackingNumber) {
            const res = await updateTrackingNumberApi({
                id, trackingNumber
            });
            if (res.code === 200) {
                this.$modal.msgSuccess("提交成功")
                this.getList();
            }
        },
        /** 搜索按钮操作 */
        handleQuery() {
            console.log(this.location, 'location');
            this.queryParams.province = this.location[0] ? this.location[0] : undefined;
            this.queryParams.city = this.location[1] ? this.location[1] : undefined;
            this.queryParams.district = this.location[2] ? this.location[2] : undefined;
            this.queryParams.pageNum = 1;
            this.queryParams.startDate = this.dateRange[0];
            this.queryParams.endDate = this.dateRange[1];
            this.getList()
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.dateRange = [];
            this.location = [];
            this.queryParams = {
                pageNum: 1,
                pageSize: 10,
                userName: undefined,
                productName: undefined,
                startDate: "",
                endDate: "",
                paymentStatus: undefined,
                logisticsStatus: undefined,
                trackingNumber: undefined,
                province: undefined,
                city: undefined,
                district: undefined,
                detailAddress: undefined,
                receiverPhone: undefined,
                receiverName: undefined,
            }
            this.resetForm("queryForm")
            this.handleQuery()
        },
    }
}
</script>
<style lang="scss" scoped>
.app-container {
    height: calc(100vh - 120px);
    // border: 1px solid #222;
}
</style>